package service;

import dao.DepartmentDao;
import domain.Department;
import domain.Teacher;
import exception.BysjException;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.SQLException;

public class DepartmentService {
    private static final DepartmentDao departmentDao = DepartmentDao.getInstance();
    private static DepartmentService departmentService = null;
    //将唯一构造器设置成私有，阻止在本类外生成本类的对象
    public static DepartmentService getInstance(){
        //静态变量SchoolDao为null时，才创建SchoolDao对象
        //第二次或后来调用本方法就不指向null了，就不在创建对象。
        if (departmentService == null){
            departmentService = new DepartmentService();
        }
        return departmentService;
    }
    public Department find(int id) throws SQLException {
        Department department = DepartmentDao.getInstance().find(id);
        return department;
    }
    public boolean add(Department department)throws SQLException , BysjException {
        Connection connection = JdbcHelper.getConn();
        boolean added = DepartmentDao.add(connection,department);
        JdbcHelper.close(connection);
        return added;
    }
    public boolean update(Department department)throws SQLException , BysjException {
        Connection connection = JdbcHelper.getConn();
        boolean updated = DepartmentDao.update(connection,department);
        JdbcHelper.close(connection);
        return updated;
    }
    public boolean delete(int id)throws SQLException , BysjException {
        boolean deleted = departmentDao.delete(id);
        return deleted;
    }
}
